package com.sl.diary.component;

import java.util.Calendar;
import java.util.Date;

import com.sl.diary.R;
import com.sl.diary.component.wheelscroll.ArrayWheelAdapter;
import com.sl.diary.component.wheelscroll.NumericWheelAdapter;
import com.sl.diary.component.wheelscroll.OnWheelChangedListener;
import com.sl.diary.component.wheelscroll.WheelView;
import com.sl.diary.util.Constant;
import com.sl.diary.util.DLog;
import com.sl.diary.util.Util;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources.Theme;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;

public class DatePickerDialog extends Dialog {

	private Context mContext;

	private int NoOfYear = 40;
	final WheelView day, month, year;
	TextView tv_date;

	public DatePickerDialog(Context context, int theme, Calendar calendar,
			Date selectedDate, final DatePickerListner dtp) {

		super(context, theme);
		// hide title bar in dialog
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		// set context
		mContext = context;
		setContentView(R.layout.date_picker);
		getWindow().setLayout(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);

		// text view
		tv_date = (TextView) findViewById(R.id.tv_datepicker_date);

		// month wheel
		month = (WheelView) findViewById(R.id.datepicker_month);
		this.setMonth(calendar, selectedDate);
		month.addChangingListener(listener);

		// year wheel
		year = (WheelView) findViewById(R.id.datepicker_year);
		this.setYear(calendar, selectedDate);
		year.addChangingListener(listener);

		// day wheel
		day = (WheelView) findViewById(R.id.datepicker_day);
		Calendar selectedCal = updateDays(year, month, day);
		this.updateTvDate(selectedCal);
		this.setDay(calendar, selectedDate);
		day.addChangingListener(listener);

		// buttons
		Button btn_done = (Button) findViewById(R.id.btn_datepicker_done);
		btn_done.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Calendar c = updateDays(year, month, day);
				dtp.OnDoneButton(DatePickerDialog.this, c);
			}
		});
		Button btn_cancel = (Button) findViewById(R.id.btn_datepicker_cancel);
		btn_cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dtp.OnCancelButton(DatePickerDialog.this);

			}
		});
		// hide cancel button if selected date is null
		if(selectedDate == null){
			btn_cancel.setVisibility(View.INVISIBLE);
		}else{
			btn_cancel.setVisibility(View.VISIBLE);
		}

	}

	private void setMonth(Calendar calendar, Date selectedDate) {
		if (selectedDate != null) {
			month.setCurrentItem(selectedDate.getMonth());
		} else {
			int curMonth = calendar.get(Calendar.MONTH);
			month.setViewAdapter(new DateArrayAdapter(mContext,
					Constant.months, curMonth));
			month.setCurrentItem(curMonth);
		}

	}

	private void setYear(Calendar calendar, Date selectedDate) {
		if (selectedDate != null) {
			month.setCurrentItem(selectedDate.getYear());
		} else {
			Calendar cal = Calendar.getInstance();
			int curYear = calendar.get(Calendar.YEAR);
			int Year = cal.get(Calendar.YEAR);
			year.setViewAdapter(new DateNumericAdapter(mContext, Year
					- NoOfYear, Year + NoOfYear, NoOfYear));
			year.setCurrentItem(curYear - (Year - NoOfYear));
		}

	}

	private void setDay(Calendar calendar, Date selectedDate) {
		if (selectedDate != null) {
			day.setCurrentItem(selectedDate.getDate() - 1);
		} else {
			day.setCurrentItem(calendar.get(Calendar.DAY_OF_MONTH) - 1);
		}
	}

	private final void updateTvDate(Calendar calendar) {
		Date selectedDate = new Date(calendar.get(Calendar.YEAR) - 1900,
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
		tv_date.setText(Util.convertDateToString(selectedDate));
	}

	Calendar updateDays(WheelView year, WheelView month, WheelView day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR,
				calendar.get(Calendar.YEAR)
						+ (year.getCurrentItem() - NoOfYear));
		calendar.set(Calendar.MONTH, month.getCurrentItem());

		int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		day.setViewAdapter(new DateNumericAdapter(mContext, 1, maxDays,
				calendar.get(Calendar.DAY_OF_MONTH) - 1));
		int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
		day.setCurrentItem(curDay - 1, true);
		calendar.set(Calendar.DAY_OF_MONTH, curDay);
		return calendar;

	}

	private class DateNumericAdapter extends NumericWheelAdapter {
		int currentItem;
		int currentValue;

		public DateNumericAdapter(Context context, int minValue, int maxValue,
				int current) {
			super(context, minValue, maxValue);
			this.currentValue = current;
			setTextSize(20);
		}

		@Override
		protected void configureTextView(TextView view) {
			super.configureTextView(view);
			if (currentItem == currentValue) {
				view.setTextColor(0xFF0000F0);
			}
			view.setTypeface(null, Typeface.BOLD);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			currentItem = index;
			return super.getItem(index, cachedView, parent);
		}
	}

	private class DateArrayAdapter extends ArrayWheelAdapter<String> {
		int currentItem;
		int currentValue;

		public DateArrayAdapter(Context context, String[] items, int current) {
			super(context, items);
			this.currentValue = current;
			setTextSize(20);
		}

		@Override
		protected void configureTextView(TextView view) {
			super.configureTextView(view);
			if (currentItem == currentValue) {
				view.setTextColor(0xFF0000F0);
			}
			view.setTypeface(null, Typeface.BOLD);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			currentItem = index;
			return super.getItem(index, cachedView, parent);
		}
	}

	public interface DatePickerListner {
		public void OnDoneButton(Dialog datedialog, Calendar c);

		public void OnCancelButton(Dialog datedialog);
	}

	OnWheelChangedListener listener = new OnWheelChangedListener() {
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			Calendar selectedCal = updateDays(year, month, day);
			updateTvDate(selectedCal);
		}
	};

}
